package com.redis;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisTest {

	@Test
	public void test() {
		Jedis jedis=new Jedis("127.0.0.1", 6379);
		jedis.set("address", "beijing");
		jedis.close();
	}
	
	@Test
	public void pool(){
		JedisPoolConfig config=new JedisPoolConfig();
		config.setMaxIdle(30);
		config.setMaxTotal(50);
		
		JedisPool jp=new JedisPool(config, "127.0.0.1", 6379);
		Jedis jedis=jp.getResource();
		String address=jedis.get("address");
		System.out.println(address);
		jp.returnResource(jedis);
	}
	
	@Test
	public void test1() {
		Jedis jedis=new Jedis("127.0.0.1", 6379);
		//jedis.set("address", "beijing");
		long strlen=jedis.strlen("address");
		System.out.println(jedis.get("address")+": "+strlen);
		jedis.close();
	}
	
	@Test
	public void test2() {
		Jedis jedis=new Jedis("127.0.0.1", 6379);
		jedis.mset(new String[]{"name","zhangsan","age","20"});
		jedis.close();
	}
	
	@Test
	public void test3() {
		Jedis jedis=new Jedis("127.0.0.1", 6379);
		jedis.hset("person", "name", "zhangsan");
		String name=jedis.hget("person", "name");
		//System.out.println(name);
		jedis.flushAll();
		Map<String,String> map=new HashMap<String,String>();
		map.put("name", "zhangshan");
		map.put("age", "20");
		map.put("address", "beijing");
		jedis.hmset("user", map);
		
		List<String> list=jedis.hmget("user", new String[]{"name","age","address"});
		System.out.println(list);
		jedis.close();
	}
	
	@Test
	public void test4() {
		Jedis jedis=new Jedis("127.0.0.1", 6379);
		jedis.flushAll();
		jedis.zadd("myset2",100, "哈哈");
		Map<String,Double> map=new HashMap<String,Double>();
		map.put("小米", 123d);
		map.put("华为", 1563d);
		jedis.zadd("myset2", map);
		Double zscore=jedis.zscore("myset2", "华为");
		System.out.println(zscore);
		Set<String> set=jedis.zrange("myset2", 0, -1);
		System.out.println(set);
		jedis.close();
	}

}
